﻿using System;
using System.Collections.Generic;

namespace Kangaroo.Shell
{
    class ViewManager
    {
        private readonly Func<string, IView> _viewCreator;
        private readonly IDictionary<string, IView> _availableViews = new Dictionary<string, IView>();

        public ViewManager(Func<string, IView> viewCreator)
        {
            _viewCreator = viewCreator;
        }

        public IView CurrentView { get; private set; }

        public void ChangeView(string viewKey)
        {
            if(CurrentView != null)
            {
                CurrentView.Deactivate();
            }

            CurrentView = _availableViews.TryGetValue(viewKey).ValueOrDefault(() =>
            {
                var view = _viewCreator(viewKey);
                _availableViews.Add(viewKey, view);
                return view;
            });

            CurrentView.Activate();
            CurrentView.Refresh();
        }
    }
}
